package com.syp.auth.export.common;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class TokenSecurity {

	private static final TokenSecurity instunce = new TokenSecurity();

	private static final String CHARSET_UTF8 = "UTF-8";

	private final String sKey;
	private final String sIv;

	private TokenSecurity() {
		/*
		 * String CHRS = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; int sKeyLen = 128; int sIvLen = 16; //初始化sKey StringBuilder sb = new StringBuilder(sKeyLen); for (int i = 0; i < sKeyLen; i++) { Random r = new Random(); int x = r.nextInt(CHRS.length() - 1); sb.append(CHRS.charAt(x)); }
		 * 
		 * //初始化sIv this.sKey = sb.toString(); sb = new StringBuilder(sIvLen); for (int i = 0; i < sIvLen; i++) { Random r = new Random(); int x = r.nextInt(CHRS.length() - 1); sb.append(CHRS.charAt(x)); } this.sIv = sb.toString();
		 */
		this.sKey = "qo4QG5diCxdzOIqIAbEapRiNsAGkoBXeWHfKfpeRsMdHKs1QnjGcIC7ERMkwD3VueLTcob0RroMiHU3C6jaOuuPydXMrxVAydiS6ybUPbduiN9FO6bnJVEY2j248J7os";
		this.sIv = "NQSajJi0fbmDNsUx";

	}

	public static TokenSecurity getInstunce() {
		return TokenSecurity.instunce;
	}

	/**
	 * 加密
	 * 
	 * @param strKey
	 * @param val
	 * @return
	 * @throws Exception
	 */
	public String encrypt(String text) {
		try {
			SecretKeySpec skeySpec = getKey(this.sKey);
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			IvParameterSpec iv = new IvParameterSpec(this.sIv.getBytes(CHARSET_UTF8));
			cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
			byte[] encrypted = cipher.doFinal(text.getBytes(CHARSET_UTF8));
			return bytesToHexString(encrypted);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 解密
	 * 
	 * @param text
	 * @return
	 */
	public String decrypt(String text) {
		try {
			SecretKeySpec skeySpec = getKey(this.sKey);
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			IvParameterSpec iv = new IvParameterSpec(this.sIv.getBytes(CHARSET_UTF8));
			cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
			byte[] encrypted1 = hexStringToBytes(text);
			byte[] original = cipher.doFinal(encrypted1);
			String originalString = new String(original);
			return originalString;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static SecretKeySpec getKey(String strKey) throws Exception {
		byte[] arrBTmp = strKey.getBytes();
		byte[] arrB = new byte[16]; // 创建一个空的16位字节数组（默认值为0）

		for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
			arrB[i] = arrBTmp[i];
		}

		SecretKeySpec skeySpec = new SecretKeySpec(arrB, "AES");

		return skeySpec;
	}

	/**
	 * 字节数组转十六进制字符串
	 * 
	 * @param src
	 * @return
	 */
	private static String bytesToHexString(byte[] data) {
		StringBuilder stringBuilder = new StringBuilder("");
		if (data == null || data.length <= 0) {
			return null;
		}
		for (int i = 0; i < data.length; i++) {
			int v = data[i] & 0xFF;
			String hv = Integer.toHexString(v).toUpperCase();
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}

			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
	}

	/**
	 * 十六进制字符串转字节数组
	 * 
	 * @param hexStr
	 * @return
	 */
	private static byte[] hexStringToBytes(String hexStr) {
		if (hexStr == null || hexStr.equals("")) {
			return null;
		}
		hexStr = hexStr.toUpperCase();
		int length = hexStr.length() / 2;
		char[] hexChars = hexStr.toCharArray();
		byte[] d = new byte[length];
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return d;
	}

	/**
	 * 字符转字节
	 * 
	 * @param chr
	 * @return
	 */
	private static byte charToByte(char chr) {
		return (byte) "0123456789ABCDEF".indexOf(chr);
	}
	
}
